using UnityEngine;
using System.Collections;

public class Page : MonoBehaviour
{
    private Vector3 BOOK_CENTER = new Vector3(-0.24f, 0f, 0f);
    private const float PAGE_TURN_ANGLE = 180.0f;
    private const int PAGE_TURN_ANGLE_MAX = 180;

    private int index;
    private bool isTurning;
    private bool isTurnRight;
    private float pageTurnAngle;
    private bool isOnLeftSide;
    private CutPatch cutPatch;

	// Use this for initialization
	void Start ()
    {
        isTurning = false;
        isTurnRight = false;
        isOnLeftSide = false;
        cutPatch = null;
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (isTurning)
        {
            turn();
            return;
        }
	}

    public void activeRotation( bool isTurnRight)
    {
        pageTurnAngle = 0;
        isTurning = true;
        this.isTurnRight = isTurnRight;
    }

    public void turn()
    {
        float angle = 0;

        if (isTurnRight)
        {
            angle = -PAGE_TURN_ANGLE * Time.deltaTime;
            isOnLeftSide = false;
        }
        else
        {
            angle = PAGE_TURN_ANGLE * Time.deltaTime;
            isOnLeftSide = true;
        }

        this.transform.RotateAround(BOOK_CENTER, Vector3.forward, angle);

        pageTurnAngle += PAGE_TURN_ANGLE * Time.deltaTime;

        if (pageTurnAngle >= PAGE_TURN_ANGLE_MAX)
        {
            isTurning = false;
        }
    }

    public void addCutPatch()
    {
        GameObject cutPatchPrefab = (GameObject)Instantiate(Resources.Load("cutPatchPrefab"));
        cutPatchPrefab.transform.position = this.transform.position;
        cutPatchPrefab.transform.parent = this.transform;

        cutPatch = cutPatchPrefab.GetComponent<CutPatch>();
    }

    public bool getIsTurning()
    {
        return isTurning;
    }

    public bool getIsOnLeftSide()
    {
        return isOnLeftSide;
    }

    public int getIndex()
    {
        return index;
    }

    public CutPatch getCutPatch()
    {
        return cutPatch;
    }

    public void setIndex(int pIndex)
    {
        index = pIndex;
    }

}
